package com.example.administrator.qimo3no74.shopping_cart;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.qimo3no74.R;

import java.util.List;

public class ShoppingItemAdapter extends RecyclerView.Adapter<ShoppingItemAdapter.ViewHolder> {

    private List<ShoppingItem> ShoppingItems;



    static  class ViewHolder extends RecyclerView.ViewHolder{
        ImageView itemImg;
        TextView itemName;
        TextView itemCount;
        TextView itemPrice;
        TextView storeName;
        TextView itemOther;
        Button addCountButton;
        Button subtractCountButton;

        public ViewHolder(View itemView, final Context context){
            super(itemView);
            this.itemImg = itemView.findViewById(R.id.shoppingCartImageView);
            this.itemName = itemView.findViewById(R.id.shoppingCartTextViewItemName);
            this.itemCount = itemView.findViewById(R.id.shoppingCartTextViewItemCount);
            this.itemPrice = itemView.findViewById(R.id.shoppingCartTextViewItemPrice);
            this.storeName = itemView.findViewById(R.id.shoppingCartTextViewStore);
            this.itemOther = itemView.findViewById(R.id.shoppingCartTextViewItemOther);
            this.addCountButton = itemView.findViewById(R.id.addCountButton);
            this.subtractCountButton = itemView.findViewById(R.id.subtractCountButton);

            itemImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,""+itemName.getText(),Toast.LENGTH_SHORT).show();
                }
            });
            itemName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,""+itemName.getText(),Toast.LENGTH_SHORT).show();
                }
            });
            storeName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,storeName.getText()+" 还有更多精美商品",Toast.LENGTH_SHORT).show();
                }
            });
            addCountButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,"你点击了加一件按钮",Toast.LENGTH_SHORT).show();
                }
            });
            subtractCountButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,"你点击了减一件按钮",Toast.LENGTH_SHORT).show();
                }
            });



        }
    }

    public ShoppingItemAdapter(List<ShoppingItem> ShoppingItems){
        this.ShoppingItems = ShoppingItems;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shopping_cart_item,viewGroup,false);
        ViewHolder holder = new ViewHolder(view,viewGroup.getContext());
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        ShoppingItem item = ShoppingItems.get(i);
        viewHolder.itemImg.setImageResource(item.getItemImgId());
        viewHolder.itemName.setText(item.getItemName());
        viewHolder.itemCount.setText(item.getItemCount().toString());
        viewHolder.itemPrice.setText(item.getItemPrice().toString());
        viewHolder.storeName.setText(item.getStoreName());
        viewHolder.itemOther.setText(item.getItemOther());
    }

    @Override
    public int getItemCount() {
        return ShoppingItems.size();
    }

}
